<template>
  <view class="box-give-list">
    <my-header title="选择要赠送的商品"></my-header>
    <my-content class="content" :default-padding-top="10">
      <view class="take_box">
        <view class="goods_box"
              :style="[useLevelBg('item', item.level_id)]"
              @click="selected(item, index)" v-for="(item, index) in orderData" :key="item.id">
          <view class="goods_content">
            <image mode="widthFix" v-if="item.selected" class="selected" src="https://www.dingxians.cn/images/common/selected.png">
            </image>
            <image mode="widthFix" v-else class="selected" src="https://www.dingxians.cn/images/common/unselected.png"></image>

            <view class="goods-image-bg" :style="[useLevelBg('img', item.level_id)]">
              <image class="goods_image" :src="item.blind_box_prize_image"></image>
              <view
                class="level-icon"
                :style="[useLevelBg('icon', item.level_id)]"
              >
                {{ item.level_name }}
              </view>

            </view>
            <view class="goods_info">
              <view class="goods_name">{{ item.blind_box_prize_name }}</view>
              <view class="goods_price_box">
                <view>￥</view>
                <view class="goods_price_int">{{ item.price }}</view>
                <view class="goods_price_ext">参考价</view>
              </view>
              <view class="goods_recovery">
                <view class="goods_price">
                  <view>拒提价：</view>
                  <view>{{ item.recovery_price }}</view>
                  <image class="gold" mode="widthFix" src="/static/images/shop/money.png"></image>
                </view>
                <view class="goods_num">
                  <my-number-box class="number-box" v-model="item.num" :max="item.num" :min="1"></my-number-box>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      <view style="visibility: hidden;">1</view>
      <!-- <view class="freight">
          <view class="freight_title">单次提货满3件及以上包邮（偏远地区除外）</view>
          <view class="freight_rule" @click="header('/pages/content/freight')">运费规则 > </view>
        </view> -->
      <view class="bottom">
        <view class="bottom_select" @click="allQuery()">
          <image class="bottom_select_icon" src="https://www.dingxians.cn/images/common/unselected.png" v-if="!allSelected"></image>
          <image class="bottom_select_icon" src="https://www.dingxians.cn/images/common/selected.png" v-if="allSelected"></image>
          <view style="font-size: 24rpx;">全选</view>
          <view class="bottom_box">
            <view class="bottom_text">
              <view>已选</view>
              <view class="bottom_text_num" style="color: #DED000;">{{ num }}</view>
              <view class="bottom_txt">件</view>
            </view>
            <view class="bottom_title">
              <view>可拒提：</view>
              <view class="total_recovery_price" style="color: #DED000;">{{ total_recovery_price }}</view>
              <image mode="widthFix" class="gold" src="/static/images/shop/money.png"></image>
            </view>
          </view>
        </view>

        <view class="exchange_btn" v-if="num == 0 || giveStatus">确认赠送</view>
        <view class="exchange_btn exchange" v-if="num > 0 && !giveStatus" @click="give()">确认赠送</view>
      </view>

      <uni-popup ref="popup" v-show="popStatus" :mask-click="false">
        <view class="pop_box">
          <view class="pop_header">抽到不想要的商品？赠送给想要的好朋友吧</view>

          <view class="user_info">
            <view class="give_search_box">
              <input class="user_id" v-model="giveParams.user_id" placeholder="请输入用户id" />
              <view class="give_search" @click="searchUser()">搜索</view>
            </view>
            <view class="search_user" v-if="giveParams.avatar">
              <image class="search_user_avatar" :src="giveParams.avatar"></image>
              <view class="search_user_info">
                <view class="search_user_id">用户ID:{{ giveParams.user_id }}</view>
                <view>用户昵称:{{ giveParams.nickname }}</view>
              </view>
            </view>
            <view class="give_tip">
              温馨提示：商品赠送后不可撤销
            </view>
          </view>
          <textarea v-model="giveParams.remark" class="give_remark" placeholder="赠礼留言"></textarea>

        </view>
        <view class="give_btn_group">
          <view class="give_btn give_cancel" @click="cancel()">取消</view>
          <view class="give_btn give_confirm" @click="giveSubmit()">确定</view>
        </view>
      </uni-popup>
    </my-content>

  </view>
</template>

<script>
import api from '../../api/index.js'
import box from '@/mixins/box'

export default {
  mixins: [box],
  data() {
    return {
      orderData: [],
      id: '',
      allSelected: false,
      page: 1,
      lastPage: 1,
      popStatus: false,
      giveParams: {
        prize_id: '',
        user_id: '',
        remark: '',
        avatar: '',
        nickname: '',
        num: 1,
      },
      giveStatus: false,
      // 是否是福利屋商品
      isWelfare: false,
    }
  },
  onLoad(option) {
    this.id = option.id
    this.isWelfare = option.welfare === 'true'
    this.getGoods(1)
  },
  onShow() {

  },

  onReachBottom() {
    if (this.page > this.lastPage) {
      this.show('已经到底了')
      return
    }
    this.getGoods(1)
  },

  computed: {
    // 选中的商品
    num() {
      let total_num = 0
      this.orderData.filter(item => {
        return item.selected === true
      }).forEach((item) => {
        total_num += item.num
      })
      return total_num
    },
    total_recovery_price() {
      var total = 0
      this.orderData.filter(item => {
        return item.selected === true
      }).forEach((item) => {
        total += (item.recovery_price * item.num)
      })
      return total.toFixed(2)
    },
    selected_id() {
      return this.orderData.filter(item => {
        return item.selected === true
      }).map(item => {
        return item.id + ":" + item.num
      }).join(',')
    },
    selected_id2() {
      return this.orderData.filter(item => {
        return item.selected === true
      }).map(item => {
        return item.id
      })
    },
  },
  methods: {
    searchUser() {
      uni.showLoading({
        title: '搜索用户~',
      })
      api.users.search(this.giveParams.user_id).then(res => {
        if (res.code === 200) {
          uni.hideLoading()
          this.giveParams.nickname = res.data.nickname
          this.giveParams.avatar = res.data.avatar
        }
      }).catch(error => {
        console.log(error)
      })
    },
    give() {
      this.popStatus = true
      this.$refs.popup.open('center')
    },
    refresh() {
      this.page = 1
      this.getGoods(2)
    },
    giveSubmit() {
      if (!this.giveParams.user_id) {
        this.show('请输入用户id')
        return
      }
      if (this.selected_id.length === 0) {
        this.show('请选中要赠送的商品')
        return
      }
      uni.showLoading({
        title: '赠送中~',
      })
      this.giveStatus = true
      let promise
      if (this.isWelfare) {
        promise = api.welfareHouses.give({
          user_id: this.giveParams.user_id,
          id: this.selected_id2,
          remark: this.giveParams.remark,
        })
      } else {
        promise = api.blindBoxOrderPrizes.give({
          user_id: this.giveParams.user_id, params: this.selected_id, remark: this.giveParams.remark,
        })
      }
      promise.then(res => {
        uni.hideLoading()
        this.giveStatus = false
        if (res.code === 200) {
          this.show('赠送成功')
          this.cancel()
          setTimeout(() => {
            uni.navigateBack()
          }, 1000)
        }
      }).catch(error => {
        console.log(error)
      })
    },
    cancel() {
      this.$refs.popup.close()
      this.popStatus = false
    },
    splitNum(num) {
      // if (typeof(num) == 'number') {
      return num.toString().split('.')
      // }
      // return num.split('.')

    },
    allQuery() {
      this.allSelected = !this.allSelected
      for (let i = 0; i < this.orderData.length; i++) {
        this.orderData[i].selected = this.allSelected
        this.$set(this.orderData, i, this.orderData[i])
      }
    },
    selected(item, index) {
      item.selected = !item.selected
      this.$set(this.orderData, index, item)
    },
    getGoods(type) {
      uni.showLoading({
        title: '数据加载中',
      })
      let promise
      if (this.isWelfare) {
        promise = api.welfareHouses.giveShow({ page: this.page })
      } else {
        console.log(1222)
        promise = api.blindBoxOrderPrizes.giveShow({ page: this.page })
      }
      promise.then(res => {
        if (res.code === 200) {
          uni.hideLoading()
          if (type === 1) {
            this.orderData = this.orderData.concat(res.data.data)
          } else if (type === 2) {
            this.orderData = res.data.data
          }
          this.lastPage = res.data.lastPage
          this.page++
        }
      }).catch(error => {
        console.log(error)
      })
    },

  },
}
</script>

<style lang="scss">
.box-give-list {
  min-height: 100vh;
  height: 100%;
  // background-color: #f7f7f7;
  background: #141414;
}

.content {

  .take_box {
    display: flex;
    flex-direction: column;
    margin: 0 20rpx;
    // border-radius: 5rpx;
    padding-top: 20rpx;
    margin-bottom: 200rpx;

    .goods_box {
      display: flex;
      border-radius: 10rpx;
      margin-bottom: 20rpx;
      flex-direction: column;
      background-size: 100% 100%;
      padding: 30rpx 0;
      color: white;

      .goods_level {
        color: #fff;
        font-size: 24rpx;
        padding: 10rpx 0;
        margin-top: 10rpx;
        border-top-right-radius: 20rpx;
        width: 100rpx;
        border-bottom-right-radius: 20rpx;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .goods_content {
        align-items: center;
        display: flex;
        height: 200rpx;

        .selected {
          width: 32rpx;
          height: 32rpx;
          /* margin: 0 20rpx; */
          margin-left: 40rpx;
        }

        .goods-image-bg {
          position: relative;
          background-size: 100% 100%;
          width: 200rpx;
          height: 200rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          margin: 0 20rpx;
          /* margin-right: 20rpx; */
        }

        .goods_image {
          width: 170rpx;
          height: 170rpx;
        }

        .level-icon {
          width: 71rpx;
          height: 28rpx;
          line-height: 28rpx;
          position: absolute;
          background-size: 100% 100%;
          left: 10rpx;
          top: 10rpx;
          font-size: 17rpx;
          box-sizing: border-box;
          padding-left: 30rpx;
        }

        .goods_info {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          flex: 1;
          height: 170rpx;

          .goods_name {
            width: 350rpx;
            font-size: 32rpx;
            -webkit-line-clamp: 2;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            overflow: hidden;
          }

          .goods_price_box {
            display: flex;
            align-items: center;
            font-size: 24rpx;

            .goods_price_ext {
              background-color: rgba(255, 255, 255, 0.2);
              font-size: 22rpx;
              margin-left: 10rpx;
              padding: 0 10rpx;
              text-align: center;
              color: white;
              border-radius: 5rpx;
            }
          }

          .goods_recovery {
            display: flex;
            align-items: center;
            font-size: 24rpx;
            justify-content: space-between;

            .goods_price {
              color: #ded000;
              display: flex;
              align-items: center;

              .gold {
                width: 30rpx;
                margin-left: 10rpx;
              }
            }

            .goods_num {
              font-size: 24rpx;
              color: #ffffff;
              margin-right: 30rpx;
            }
          }

        }
      }

    }
  }

  .bottom {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 120rpx;
    background-color: #141414;
    align-items: center;
    color: white;
    font-weight: bold;

    .bottom_select {
      display: flex;
      align-items: center;

      .bottom_select_icon {
        width: 32rpx;
        height: 32rpx;
        margin: 0 20rpx;
      }
    }

    .bottom_box {
      display: flex;
      flex-direction: column;
      font-size: 24rpx;
      font-weight: normal;
      margin-left: 70rpx;

      .bottom_text {
        display: flex;
        align-items: center;

        .bottom_text_num {
          margin: 0 10rpx;
          /* font-weight: bold; */
          font-size: 24rpx;
        }
      }

      .bottom_title {
        display: flex;
        align-items: center;

        .total_recovery_price {
          /* font-weight: bold; */
          font-size: 24rpx;
        }

        .gold {
          width: 30rpx;
          height: 30rpx;
          margin-left: 10rpx;
        }
      }
    }

    .exchange_btn {
      height: 84rpx;
      line-height: 84rpx;
      text-align: center;
      width: 228rpx;
      margin: 0 20rpx;
      border-radius: 5rpx;
      font-size: 24rpx;
      background-size: 100% 100%;
      background-image: url('https://www.dingxians.cn/upload/user_static/common/btn-primary-small.png');
      filter: grayscale(1);

      &.exchange {
        filter: none;
        color: #fff;
      }
    }
  }

  .pop_box {
    display: flex;
    flex-direction: column;
    background-image: url('https://www.dingxians.cn/upload/user_static/box/bg-give.png');
    background-size: 100% 100%;
    border-radius: 10rpx;
    width: 655rpx;
    height: 750rpx;

    .pop_header {
      font-size: 24rpx;
      height: 50rpx;
      line-height: 50rpx;
      text-align: center;
      margin-top: 100rpx;
      color: white;
    }

    .user_info {
      display: flex;
      flex-direction: column;

      .give_search_box {
        display: flex;
        justify-content: center;
        font-size: 24rpx;
        margin: 0 auto;
        width: 542rpx;
        height: 80rpx;
        align-items: center;
        background-color: #141414;
        border: 1rpx solid #333122;
        border-radius: 2rpx;


        .user_id {
          flex: 1;
          border-radius: 10rpx;
          margin-right: 10rpx;
          padding: 0 24rpx;
          font-size: 24rpx;
          color: white;

          &::placeholder {
            color: #525252;
          }
        }

        .give_search {
          width: 112rpx;
          height: 52rpx;
          border-radius: 2rpx;
          background-color: #ded000;
          margin-right: 12rpx;
          text-align: center;
          line-height: 52rpx;
          font-weight: bold;
          color: #141414;
        }
      }

      .search_user {
        font-size: 24rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 20rpx 0 0;

        .search_user_avatar {
          border-radius: 50%;
          width: 80rpx;
          height: 80rpx;
          margin-right: 20rpx;
          margin-bottom: 20rpx;
        }

        .search_user_info {
          display: flex;
          height: 80rpx;
          flex-direction: column;
          justify-content: space-between;
          color: white;

          .search_user_id {
            color: red;
          }
        }
      }

      .give_tip {
        margin: 20rpx 0;
        font-size: 24rpx;
        color: red;
        text-align: center;
      }
    }

    .give_remark {
      margin: 0 auto;
      box-sizing: border-box;
      padding: 32rpx 24rpx;
      width: 542rpx;
      background-color: #141414;
      border: 1rpx solid #333122;
      border-radius: 2rpx;

      font-size: 24rpx;
      color: white;

      &::placeholder {
        color: #525252;
      }
    }

  }

  .give_btn_group {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 32rpx 30rpx 30rpx 30rpx;

    .give_btn {
      width: 228rpx;
      height: 84rpx;
      background-size: 100% 100%;
      text-align: center;
      line-height: 84rpx;
      border-radius: 10rpx;
      font-size: 30rpx;
      background-image: url('https://www.dingxians.cn/upload/user_static/common/btn-primary-small.png');
      color: #fff;

      &.give_cancel {
        filter: grayscale(1);
      }

      &.give_confirm {
        margin-left: 56rpx;

      }
    }

  }
}
</style>